<?php

namespace addons\Employees\common\models;


use Yii;
use yii\db\ActiveQuery;
use common\helpers\ArrayHelper;
use common\helpers\AddrHelper;
use common\helpers\StringHelper;
use Overtrue\Pinyin\Pinyin;
use yii\helpers\Json;

/**
 * This is the model class for table "{{%addon_employees}}".
 *
 * @property int $id
 * @property string $name
 * @property int $age
 * @property string $sex
 * @property string $birthday 生日
 * @property string $idcard 身份证
 * @property string $native 籍贯
 * @property string $political 政治面貌
 * @property string $education 学历
 * @property string $colleges 毕业院校
 * @property string $residence 户口所在地
 * @property string $mobile 手机
 * @property string $mail 邮箱
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 修改时间
 */
class Employees extends \common\models\base\BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%addon_employees}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['serial','name', 'idcard'], 'required'],
            [['merchant_id','status','gender','political'], 'integer'],
            [['picture'], 'string','max' => 255],
            [['serial', 'name','pinyin', 'native',  'education', 'colleges', 'residence', 'mobile', 'mail'], 'string', 'max' => 32],
            [['serial'], 'unique', 'filter' => function (ActiveQuery $query) {
                return $query->all();
            }],
            [['idcard'], 'unique', 'filter' => function (ActiveQuery $query) {
                return $query->all();
            }],
            // 定义为匿名函数的行内验证器
            ['idcard', function ($attribute, $params) {
                if (!StringHelper::isIdCard($this->$attribute)) {
                    $this->addError($attribute, '身份证号不正确，请重新输入！');
                }
            }],
            [['birthday','age','pinyin','covers', 'created_at', 'updated_at'], 'safe'],

        ];

    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'serial' => '员工编号',
            'name' => '姓名',
            'age' => '年龄',
            'gender' => '性别',
            'birthday' => '生日',
            'idcard' => '身份证',
            'native' => '籍贯',
            'political' => '政治面貌',
            'education' => '学历',
            'colleges' => '毕业院校',
            'residence' => '户口所在地',
            'mobile' => '手机',
            'mail' => '邮箱',
            'picture' => '主图',
            'covers' => '附件',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
        ];
    }

    /**
     * 员工下拉
     *
     * @return \yii\db\ActiveQuery
     */
    public static function getDropDown()
    {
        $models = Employees::find()
            ->select(['id', 'name','serial'])
            ->andFilterWhere(['merchant_id' => Yii::$app->user->identity->merchant_id])
            ->orderBy('serial asc,name asc')
            ->asArray()
            ->all();
        return ArrayHelper::map($models, 'id', 'name');
    }

    public function beforeSave($insert)
    {
        $pinyin = new Pinyin();
        $this->pinyin = $pinyin->abbr($this->name);
        $this->age = StringHelper::get_age($this->idcard);
        $this->birthday = StringHelper::get_birthday($this->idcard);
        $this->native = $this->native?$this->native:AddrHelper::get_shenfen($this->idcard);
        $this->residence = $this->residence?$this->residence:AddrHelper::get_addr($this->idcard);
        $this->gender = StringHelper::get_sex($this->idcard);
        $this->merchant_id = Yii::$app->user->identity->merchant_id;
        // 更新系统主图
        $covers = Json::decode($this->covers);
        $this->picture = $covers[0] ?? '';
        return parent::beforeSave($insert);
    }
}